<?php
// +----------------------------------------------------------------------
// | XiaMiCMF - 代码如诗
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2016 http://shujuwajue.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小虾米 <509129@qq.com>
// +----------------------------------------------------------------------
namespace PcHome\Controller;

use Common\Controller\AdminBaseController;

class AdminArticleCateController extends AdminBaseController
{
    const CNAME = '栏目';

    protected $category_model;

    public function _initialize() {
        parent::_initialize();
        $this->category_model = D("ArticleCate");
        $this->assign('TITLE', self::CNAME);
    }

    /**
     * 栏目列表
     */
    public function index ()
    {
        $cates = $this->category_model->fetchAll();
        $cates = x_get_tree($cates, 'cate_id', 'parent_id', 'cate_name');
        $this->assign('cates', $cates);
        $this->display();
    }

    /**
     * 添加栏目
     */
    public function add() {

        $parent_id = I('parent_id', 0, 'intval');
        $cate_level = I('cate_level', 1, 'intval');

        if (IS_POST) {

            if ($cate_level > 3) {
                $this->error(self::CNAME . '分类不能超过三层');
            }

            $data = $this->category_model->create();
            if ($data) {
                $id = $this->category_model->add($data);
                if ($id) {
                    $this->success('添加成功', U('index'));
                } else {
                    $this->error('添加失败');
                }
            } else {
                $this->error($this->category_model->getError());
            }
        } else {

            $tpl_list = x_admin_get_tpl_file_list();

            $this->assign('tpl_list', $tpl_list);
            $this->assign('parent_id', $parent_id);
            $this->assign('cate_level', $cate_level);
            $this->assign('cates', $this->category_model->fetchAll());
            $this->display();
        }
    }

    /**
     * 编辑栏目
     */
    public function edit() {
        if (IS_POST) {

            $parent_id = I('parent_id', 0, 'intval');
            if($parent_id == 0) {
                $_POST['cate_level'] = 1;

            } else {
                $detail = $this->category_model->find($parent_id);
                $_POST['cate_level'] = $detail['cate_level'] + 1;
            }

            $data = $this->category_model->create();
            if ($data) {
                if ($this->category_model->save($data) !== false) {
                    $this->success('编辑成功', U('index'));
                } else {
                    $this->error('编辑失败');
                }
            } else {
                $this->error($this->category_model->getError());
            }
        } else {

            $tpl_list = x_admin_get_tpl_file_list();
            $this->assign('tpl_list', $tpl_list);

            $cate_id = I('get.cate_id',0,'intval');
            $cates = $this->category_model->fetchAll();
            $this->assign('cates', x_get_tree($cates, 'cate_id', 'parent_id', 'cate_name'));
            $detail = $this->category_model->find($cate_id);
            $this->assign('detail', $detail);
            $this->display();
        }
    }

    /**
     * 删除栏目
     */
    public function delete() {
        $cate_id = I('post.cate_id',0,'intval');

        if (is_numeric($cate_id)) {
            $this->category_model->delete($cate_id);
            $this->success('删除成功', U('index'));
        } else {
            $this->error('请选择要删除的' . self::CNAME);
        }
    }

    /**
     * 更新排序
     */
    public function updateSort() {

        $status = parent::_updateListSort($this->category_model);
        if ($status) {
            $this->success("更新排序成功", U('index'));
        } else {
            $this->error("更新排序失败");
        }
    }

    /**
     * 改变显示状态
     */
    public function changeShow()
    {
        $cate_id = I('post.cate_id',0,'intval');

        if (!empty($cate_id)) {
            if (! $detail = $this->category_model->find($cate_id)) {
                $this->error('请选择要改变显示状态的' . self::CNAME);
            }

            $data['cate_id'] = $cate_id;
            $data['status'] = $detail['status'] ? 0 : 1 ;
            if ($this->category_model->save($data) !== false) {
                $this->success('操作成功', U('index'));
            } else {
                $this->error('操作失败');
            }
        } else {
            $this->error('请选择要改变显示状态的' . self::CNAME);
        }
    }
}